package com.lynn.salary.web.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.SystemArgument;
import com.lynn.salary.entity.User_;
import com.lynn.salary.pojo.FeedBack;
import com.lynn.salary.pojo.TreeNode;
import com.lynn.salary.service.IUserService;
import com.lynn.salary.util.ObjectFactory;

public class UserAction extends BaseAction {

	IUserService userService = (IUserService) ObjectFactory.getBean("userService");

	private List<TreeNode> all;
	private String flag;
	private String empId;
	private String empName;
	private FeedBack feedBack;
	private String itemId;

	private List<SystemArgument> quick;
	private List<Map<String, Object>> sp;
	
	private User_ user;
	private String oldPassword;
	private String [] ids;
	private String msg;

	public String showTree() throws Exception {
		all = userService.findAll();
		if (flag != null && flag.equals("dep")) {
			for (int i = 0; i < all.size(); i++) {
				TreeNode t = (TreeNode) all.get(i);
				t.setChilds(null);
			}
		}
		return "showTree";
	}
	
	public String register() throws Exception {
		if(flag == null || flag.trim().equals("")){//显示注册页面
			return "showRegisterPage";
		}
		
		String email = user.getEmail();
		user.setId(email);
		user.setName(email);
		user.setRegisterTime(new Date());
		
		boolean isExist = userService.checkIfUserExist(user.getId());
		
		if(isExist) {
			return "userAlreadyExist";
		}
		
		userService.save(user);
		
		return SUCCESS;
	}
	
	public String undoFbd() throws Exception {
		boolean flag;
		try {
			flag = userService.removeTag(ids, User_.FORBIDDEN, new User_());
			if(flag)
			flag = userService.addTag(ids, User_.NORMAL, new User_());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		
		feedBack = new FeedBack();
		if(flag){
			feedBack.setStatusCode("200");
			feedBack.setMessage("解禁成功");
		} else {
			feedBack.setStatusCode("300");
			feedBack.setMessage("解禁失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allUser");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}
	
	public String fbd() throws Exception {
		boolean flag;
		try {
			flag = userService.removeTag(ids, User_.NORMAL, new User_());
			if(flag)
			flag = userService.addTag(ids, User_.FORBIDDEN, new User_());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		
		feedBack = new FeedBack();
		if(flag){
			feedBack.setStatusCode("200");
			feedBack.setMessage("禁用成功");
		} else {
			feedBack.setStatusCode("300");
			feedBack.setMessage("禁用失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allUser");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}
	
	public String changepwd() throws Exception {
		
		if(flag == null || flag.trim().equals("")){
			return "showChangePwd";
		}
		User_ lg = loginUser.getLoginUser();
		if(!oldPassword.equals(lg.getPassword())){
			feedBack = new FeedBack();
			feedBack.setStatusCode("300");
			feedBack.setMessage("旧密码错误!");
			feedBack.setRel("");
			feedBack.setNavTabId("");
			feedBack.setForwardUrl("");
			feedBack.setCallbackType("");
			return "feedback";
		}
		
		try {
			Map<String,Object> data = new HashMap<String, Object>();
			data.put("id", lg.getId());
			data.put("password", lg.getPassword());
	 
			userService.update(data, User_.class);
			lg.setPassword(user.getPassword());
		} catch (Exception e) {
//			e.printStackTrace();
			feedBack = new FeedBack();
			feedBack.setStatusCode("300");
			feedBack.setMessage("密码修改失败");
			feedBack.setRel("");
			feedBack.setNavTabId("");
			feedBack.setForwardUrl("");
			feedBack.setCallbackType("");
			return "feedback";
		}
		
		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("密码修改成功");
		feedBack.setRel("");
		feedBack.setNavTabId("allUser");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}
	
	public String newRecommend() throws Exception {
		if(flag == null || flag.trim().equals("")) {
			user = new User_();
			user.setGender("男");
			return "input";
		}
		
		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("创建成功");
		feedBack.setRel("changepwd");
		feedBack.setNavTabId("");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("closeCurrent");

		return "feedback";
	}
	
	public String search() throws Exception {
		
		return "search";
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public List<TreeNode> getAll() {
		return all;
	}

	public void setAll(List<TreeNode> all) {
		this.all = all;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<SystemArgument> getQuick() {
		return quick;
	}

	public void setQuick(List<SystemArgument> quick) {
		this.quick = quick;
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getMsg() {
		return msg;
	}

	public List<Map<String, Object>> getSp() {
		return sp;
	}

	public void setSp(List<Map<String, Object>> sp) {
		this.sp = sp;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public User_ getUser() {
		return user;
	}

	public FeedBack getFeedBack() {
		return feedBack;
	}

	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setUser(User_ user) {
		this.user = user;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
}
